package com.miku.blog.config;

import com.miku.blog.properties.HttpClientProperties;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @author shkstart
 * @create ${}YEAR-05-25 20:39
 */
@EnableConfigurationProperties(HttpClientProperties.class)
@Configuration
public class HttpClientConfig {

    @Autowired
    private HttpClientProperties httpClientProperties;

    @Bean
    public RestTemplate getRestTemplate(){
        RestTemplate restTemplate = new RestTemplate();
        List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();

        for (HttpMessageConverter<?> messageConverter : messageConverters) {
            if (messageConverter instanceof StringHttpMessageConverter){

                StringHttpMessageConverter stringHttpMessageConverter
                        = (StringHttpMessageConverter) messageConverter;

                stringHttpMessageConverter.setDefaultCharset(StandardCharsets.UTF_8);
                break;
            }
        }

        restTemplate.setRequestFactory(getClientHttpRequestFactory());


        return restTemplate;
    }

    private ClientHttpRequestFactory getClientHttpRequestFactory(){
        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
        factory.setHttpClient(getHttpClient());
        return factory;
    }


    private HttpClient getHttpClient(){
        CloseableHttpClient httpClient = HttpClientBuilder.create()
                .setConnectionManager(getHttpClientConnectionManager())
                .setDefaultRequestConfig(getRequestConfig())
                .build();

        return httpClient;
    }


    private RequestConfig getRequestConfig(){
        RequestConfig requestConfig
                = RequestConfig.custom()
                .setConnectTimeout(httpClientProperties.connectTimeout)
                .setConnectionRequestTimeout(httpClientProperties.connectionRequestTimeout)
                .setSocketTimeout(httpClientProperties.socketTimeout)
                .build();

        return requestConfig;
    }

    private HttpClientConnectionManager getHttpClientConnectionManager(){
        PoolingHttpClientConnectionManager connectionManager
                = new PoolingHttpClientConnectionManager();
        connectionManager.setDefaultMaxPerRoute(httpClientProperties.defaultMaxPerRoute);
        connectionManager.setMaxTotal(httpClientProperties.maxTotal);

        return connectionManager;
    }



}
